package com.example.demo.controller;

import com.example.demo.entity.dto.ApiSuiteTreeDto;
import com.example.demo.entity.ApiSuite;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.ApiSuiteService;
import com.example.demo.service.ProjectService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/apiSuite")
@Api(tags = "api类别管理")
public class ApiSuiteController {
    @Resource
    private ApiSuiteService apiSuiteService;
    @Resource
    private ProjectService projectService;

    /**
     * 添加api类别
     *
     * @param apiSuite api类别
     * @return 获取返回结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加api类别")
    public ResponseInfo<String> insertApiSuite(@RequestBody ApiSuite apiSuite) {
        List<ApiSuite> apiSuites = apiSuiteService.findAllByName(apiSuite.getName());
        if (apiSuites.size() > 0) {
            return ResponseInfo.errorInfo("apiSuiteName已重复");
        }
        apiSuiteService.insertApiSuite(apiSuite);
        return ResponseInfo.successInfo("");
    }

    /**
     * 更新apiSuite
     *
     * @param apiSuite api类别
     * @return 获取返回结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新测试用例类别")
    public ResponseInfo<String> updateApiSuite(@RequestBody ApiSuite apiSuite) {
        ApiSuite apiSuites = apiSuiteService.findAllById(apiSuite.getId());
        if (apiSuites.getId() == 0) {
            return ResponseInfo.errorInfo("apiSuite不存在");
        }
        apiSuiteService.updateApiSuite(apiSuite);
        return ResponseInfo.successInfo("");
    }

    /**
     * 删除apiSuite
     *
     * @param apiSuiteId api类别Id
     * @return 获取返回结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除测试用例类别")
    public ResponseInfo<String> deleteApiSuiteById(@PathVariable(value = "id") int apiSuiteId) {
        apiSuiteService.deleteById(apiSuiteId);
        return ResponseInfo.successInfo("");
    }


    /**
     * 测试用例分页接口
     *
     * @param pageNum   页数
     * @param pageSize  每页长度
     * @param projectId 项目id
     * @return 获取返回结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "测试用例分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "int"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = false, dataType = "int"),
    })
    public ResponseInfo<PageInfoNew<ApiSuite>> finAllWithPage(int pageNum,
                                                              int pageSize,
                                                              @RequestParam(value = "projectId",required = false) int projectId) {
        return ResponseInfo.successInfo(apiSuiteService.findAllWithPage(pageNum, pageSize, projectId));
    }

    /**
     * api详情获取接口
     *
     * @param apiSuiteId api类别Id
     * @return 获取返回结果
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "获取api详情接口")
    public ResponseInfo<ApiSuite> findById(@PathVariable(value = "id") int apiSuiteId) {
        return ResponseInfo.successInfo(apiSuiteService.findAllById(apiSuiteId));
    }

    /**
     * 根据项目id获取api类别列表
     *
     * @param projectId 项目Id
     * @return 获取返回结果
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据项目id获取api类别列表")
    public ResponseInfo<List<ApiSuite>> findByProjectId(@RequestParam(value = "projectId") int projectId) {
        return ResponseInfo.successInfo(apiSuiteService.findByProjectId(projectId));
    }


    /**
     * api分类树状图接口
     *
     * @param projectId 项目Id
     * @return 返回结果
     */
    @GetMapping("/listTree")
    @ApiOperation(value = "根据项目id获取api类别树")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = ApiSuiteTreeDto.class)
    })
    public ResponseInfo<List<ApiSuiteTreeDto>> findTreeDtoByProjectId(@RequestParam(value = "projectId") int projectId) {
        return ResponseInfo.successInfo(apiSuiteService.findTreeDtoByProjectId(projectId));
    }
}

